﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dycox.ComponentModel;

namespace Dycox.Windows.Forms
{
    public class DetailsViewItem : ListViewItem
    {
        protected static readonly Attribute[] Attributes = new Attribute[] {
            BrowsableAttribute.Yes, VisibleAttribute.Yes };

        protected DetailsViewItem(string name)
            : base(name)
        {
        }

        public string ItemName
        {
            get { return this.Text; }
            set { this.Text = value; }
        }

        public string ItemText
        {
            get
            {
                if (SubItems.Count > 1)
                    return SubItems[1].Text;
                else
                    return string.Empty;
            }
            set
            {
                if (SubItems.Count == 1)
                    SubItems.Add(value);
                else
                    SubItems[1].Text = value;
            }
        }

        public virtual bool Expandable
        {
            get { return false; }
        }

        private bool _expanded;

        public bool IsExpanded
        {
            get { return _expanded; }
            internal set { _expanded = value; }
        }

        private void EnsureChildItems()
        {
            if (_items == null)
            {
                _items = new List<DetailsViewItem>();

                if (Expandable)
                {
                    PopulateItems(_items);

                    foreach (var item in _items)
                    {
                        if (object.ReferenceEquals(this, item))
                            throw new InvalidOperationException();

                        item._parent = this;
                    }
                }
            }
        }

        protected virtual void PopulateItems(List<DetailsViewItem> list)
        {
            throw new NotImplementedException();
        }

        private DetailsViewItem _parent;

        public DetailsViewItem Parent
        {
            get { return _parent; }
        }

        public int Depth
        {
            get
            {
                int d = 0;
                var p = _parent;

                while (p != null)
                {
                    d++;
                    p = p._parent;
                }

                return d;
            }
        }

        List<DetailsViewItem> _items;

        public DetailsViewItem[] Items
        {
            get
            {
                EnsureChildItems();
                return _items.ToArray();
            }
        }

        internal static DetailsViewPropertyItem CreateItem(PropertyDescriptor property, object instance)
        {
            Type type = property.PropertyType;

            if (type.GetInterface("ICollection") == typeof(System.Collections.ICollection))
                return new DetailsViewCollectionPropertyItem(property, instance);
            else
                return new DetailsViewPropertyItem(property, instance);
        }

        public bool Visible
        {
            get { return this.ListView != null; }
        }
    }
}
